package page.sample;

import java.awt.Button;
import java.awt.Label;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import app.MyFirstWebApplication;
import dao.impl.AdministratorDao;
import dao.impl.ClientDao;
import page.MyFirstWebPage;
import page.sample.AdministratorPage.AdministratorOfficePage;
import page.sample.ClientPage.PrivateOfficePage;
import entity.Client;
import entity.InfoUser;

public class AuthorizationPage extends WebPage {

	public AuthorizationPage() {
		super();
		// create FORM component which required to POST data from client->server
		final InfoUser infoUser = new InfoUser();
		final ClientDao clientDao = new ClientDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);
		final AdministratorDao administratorDao = new AdministratorDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		infoUser.setName("");
		infoUser.setSurname("");
		Form<InfoUser> form = new Form("form",
				new CompoundPropertyModel<InfoUser>(infoUser));
		form.add(new TextField<String>("login"));
		form.add(new PasswordTextField("password"));

		form.add(new SubmitLink("enter") {

			@Override
			public void onSubmit() {
				if (clientDao.get(infoUser.getLogin()) != null
						&& clientDao.get(infoUser.getLogin()).getInfoUser()
								.getLogin().equals(infoUser.getLogin())
						&& clientDao.get(infoUser.getLogin()).getInfoUser()
								.getPassword().equals(infoUser.getPassword())) {
					infoUser.setName(clientDao.get(infoUser.getLogin())
							.getInfoUser().getName());
					infoUser.setSurname(clientDao.get(infoUser.getLogin())
							.getInfoUser().getSurname());
					setResponsePage(new PrivateOfficePage(infoUser));
				} else if (administratorDao.get(infoUser.getLogin()) != null
						&& administratorDao.get(infoUser.getLogin()).getInfoUser()
								.getLogin().equals(infoUser.getLogin())
						&& administratorDao.get(infoUser.getLogin()).getInfoUser()
								.getPassword().equals(infoUser.getPassword())) {
					infoUser.setName(administratorDao.get(infoUser.getLogin())
							.getInfoUser().getName());
					infoUser.setSurname(administratorDao.get(infoUser.getLogin())
							.getInfoUser().getSurname());
					setResponsePage(new AdministratorOfficePage(infoUser));
				}
			}

		});

		form.add(new Link("registration") {

			@Override
			public void onClick() {
				setResponsePage(new RegistrationPage());
			}

		});

		add(form);

	}
}
